package moniCA::LOG::Appender::HTMLMail;

# By: Joseph Annino
# (c) 2002
# Licensed under the same terms as Perl
#

use strict;
use warnings;
use moniCA::LOG::Dispatch::Email;
use List::MoreUtils qw(uniq);
use base qw( moniCA::LOG::Dispatch::Email );
use Data::Dumper;
use Mail::Sender;

sub new {
    my $proto = shift;
    my $class = ref $proto || $proto;

    my %p = @_;
    my $smtp = delete $p{smtp} || 'localhost';

    my $self = $class->SUPER::new(%p);

    $self->{smtp} = $smtp;
    return $self;
}

sub send_email {
    my $self = shift;
	print Dumper $self;
    my %p    = @_;
    local $?;
    eval {
        my $sender = Mail::Sender->new(
            {
                from    => $self->{from} || 'LogDispatch@foo.bar',
                replyto => $self->{from} || 'LogDispatch@foo.bar',
                smtp    => $self->{smtp},
                cc    => ( join ',', @{ $self->{cc} }),
            }
        );

        die "Error sending mail ($sender): $Mail::Sender::Error"
            unless ref $sender;

ref $sender->OpenMultipart({to       => ( join ',', @{ $self->{to} } ),
                  subject  => $self->{subject},
                  ctype    => 'text/html',
                  encoding => '8bit'}) or die $Mail::Sender::Error,"\n";

  $sender->Part({ctype => 'text/html', disposition => 'NONE', msg =>"
<!DOCTYPE html PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\"
   \"http://www.w3.org/TR/html4/loose.dtd\">

<html>
<head>
<meta http-equiv=\"content-type\" content=\"text/html; charset=utf-8\" />
<style type=\"text/css\">
<!--
body 	{  
   margin: 0px;
   font-family: Arial, Helvetica, sans-serif;
   font-size: 12px;
   padding: 0px;
   border: 0px;
   background-color: #ffffff;
}

td	{color:#444444;font-size:8pt; font-family: Tele-GroteskNor,arial,verdana,geneva,helvetica; }
#WARN{background-color:#FFE600;color:#000000; FONT-FAMILY: verdana,Arial,Geneva;font-size:10pt; margin:1px; border:1px solid;}
#FATAL{background-color:#000000;color:#FFFFFF; FONT-FAMILY: verdana,Arial,Geneva;font-size:10pt; margin:1px; border:1px solid;}
#ERROR{background-color:#FF0000;color:#000000; FONT-FAMILY: verdana,Arial,Geneva;font-size:10pt; margin:1px; border:1px solid;}
#INFO{background-color:#AADD00;color:#000000; FONT-FAMILY: verdana,Arial,Geneva;font-size:10pt; margin:1px; border:1px solid #000000;}
#DEBUG{background-color:#9B30FF;color:#FFFFFF; FONT-FAMILY: verdana,Arial,Geneva;font-size:10pt; margin:1px; border:1px solid #000000;}
#TRACE{background-color:#603311;color:#FFFFFF; FONT-FAMILY: verdana,Arial,Geneva;font-size:10pt; margin:1px; border:1px solid #000000;}
.impressum {
font-family: Arial, Helvetica, sans-serif;
font-size: 10px;
}

.text {
font-size: 12px;
font-family: Arial, Helvetica, sans-serif;
}
.titel {
font-family: Tele-GroteskNor;
font-size: 30px;
}
.unterzeile {
font-family: Tele-GroteskNor;
font-size: 18px;
font-weight: bold;
}
a, a:visited, a:link, a:hover {
  text-decoration: none;
}
.noBreak {
 white-space: nowrap;
}

.head
{
color:#ca1f7b;font-size:12pt;font-weight:bold; font-family: arial,verdana,geneva,helvetica;
}
.text
{
color:#444444;font-size:10pt;font-family: arial,verdana,geneva,helvetica;
}


-->
</style>

<style type=\"text/css\">
BODY {
 BACKGROUND-IMAGE: url(cid:bg2a);
background-repeat: repeat-x no-repeat;
background-position: top center;
}
</style>

</head>
<body bgcolor=\"#E2E2E2\" text=\"#4d4d4d\" link=\"#e20074\" vlink=\"#e20074\" alink=\"#e20074\" leftmargin=\"0\" topmargin=\"0\" marginwidth=\"0\" marginheight=\"0\">
<table width=\"100%\" height=\"100%\" border=\"0\" cellpadding=\"0\" cellspacing=\"0\">
<tr>
<td valign=\"top\" class=\"text\">
<table style=\"PADDING-BOTTOM: 0px; MARGIN: 0px; PADDING-LEFT: 0px; PADDING-RIGHT: 0px; PADDING-TOP: 0px\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\" width=\"100%\" background=\"cid:bg2a\">
<tbody>
<tr>
<td align=\"middle\">
<table style=\"TEXT-ALIGN: left\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\" width=\"700\">
<tbody>
<tr>
<td>
<table border=\"0\" cellspacing=\"0\" cellpadding=\"0\" width=\"700\">
<tbody>
<tr>
<td colspan=\"3\"><img style=\"DISPLAY: block\" border=\"0\" src=\"cid:spacer\" width=\"1\" height=\"15\"/></td>
</tr>

<tr>
<td><img border=\"0\" src=\"cid:spacer\" width=\"34\" height=\"1\"/></td>
<td><img border=\"0\" src=\"cid:header_03\" width=\"632\" height=\"74\"/></td>
<td><img border=\"0\" src=\"cid:header_04\" width=\"34\" height=\"74\"/></td>
</tr>
<tr>
<td colspan=\"3\"><img style=\"DISPLAY: block\" border=\"0\" src=\"cid:spacer\" width=\"1\" height=\"15\"/></td>
</tr>

<tr>
<td></td>
<td>
<div class=head>Description:</div><div class=text>".$self->{DetailDesc}."</div>
</td>
<td></td>
</tr>
<tr>
<td colspan=\"3\"><img style=\"DISPLAY: block\" border=\"0\" src=\"cid:spacer\" width=\"1\" height=\"15\"/></td>
</tr>
<tr>
<td></td>
<td>
<div class=head>Plug Configuration :</div><div class=text>".$self->{plug}."</div>
</td>
<td></td>
</tr>
<tr>
<td colspan=\"3\"><img style=\"DISPLAY: block\" border=\"0\" src=\"cid:spacer\" width=\"1\" height=\"15\"/></td>
</tr>
<tr>
<td></td>
<td>
<div class=head>Results:</div><div class=text>".$p{message}."</div>
</td>
<td></td>
</tr>
<tr>
<td colspan=\"3\"><img style=\"DISPLAY: block\" border=\"0\" src=\"cid:spacer\" width=\"1\" height=\"15\"/></td>
</tr>


</tbody>
</table>
</td>
</tr>

<tr>
<td>
<table border=\"0\" cellspacing=\"0\" cellpadding=\"0\" width=\"632\" align=\"center\">
<tbody>

<tr>
<td width=182><div class=head>Explanation:</div></td>
<td width=150><div id=FATAL>FATAL</div><div id=ERROR>ERROR</div></td>
<td width=150><div id=WARN>WARN</div><div id=INFO>INFO</div></td>
<td width=150><div id=DEBUG>DEBUG</div><div id=TRACE>TRACE</div></td>
</tr>
<tr>
<td colspan=\"4\"><img style=\"DISPLAY: block\" border=\"0\" src=\"cid:spacer\" width=\"1\" height=\"15\"/></td>
</tr>
<tr>
<td width=182><div class=head>Development:</div></td>
<td colspan=3>
<div class=text><a href=mailto:Andre.Schmitt\@t-systems.com>Andre Schmitt</a></div>
<div class=text><a href=mailto:Carsten.Kahlmeier\@t-systems.com>Carsten Kahlmeier</a></div>
</td>
</tr>

</tbody>
</table>
</td>
</tr>

</tbody>
</table>
</td>
</tr>
</tbody>
</table>
</td>
</tr>
</table>
</body>
</html>

</html>"});

 $sender->Attach({
    description => 'header_03.jpg',
    ctype       => 'image/jpg',
    encoding    => 'base64',
    disposition => 'inline; filename="header_03.jpg";',
    content_id  => 'header_03',
    file        => $ENV{'MONICA_PRODUCT_DIR'}.'/lib/moniCA/LOG/Appender/header_03.jpg'
  });
 $sender->Attach({
    description => 'spacer.gif',
    ctype       => 'image/gif',
    encoding    => 'base64',
    disposition => 'inline; filename="spacer.gif";',
    content_id  => 'spacer',
    file        => $ENV{'MONICA_PRODUCT_DIR'}.'/lib/moniCA/LOG/Appender/spacer.gif'
  });

 $sender->Attach({
    description => 'header_04.gif',
    ctype       => 'image/gif',
    encoding    => 'base64',
    disposition => 'inline; filename="header_04.gif";',
    content_id  => 'header_04',
    file        => $ENV{'MONICA_PRODUCT_DIR'}.'/lib/moniCA/LOG/Appender/header_04.gif'
  });
 $sender->Attach({
    description => 'bg2a.gif',
    ctype       => 'image/gif',
    encoding    => 'base64',
    disposition => 'inline; filename="bg2a.gif";',
    content_id  => 'bg2a',
    file        => $ENV{'MONICA_PRODUCT_DIR'}.'/lib/moniCA/LOG/Appender/bg2a.gif'
  });


  $sender->Close();

    };

    warn $@ if $@;
}

1;

# ABSTRACT: Subclass of Log::Dispatch::Email that uses the Mail::Sender module



=pod

=head1 NAME

Log::Dispatch::Email::MailSender - Subclass of Log::Dispatch::Email that uses the Mail::Sender module

=head1 VERSION

version 2.29

=head1 SYNOPSIS

  use Log::Dispatch;

  my $log = Log::Dispatch->new(
      outputs => [
          [
              'Email::MailSender',
              min_level => 'emerg',
              to        => [qw( foo@example.com bar@example.org )],
              subject   => 'Big error!'
          ]
      ],
  );

  $log->emerg("Something bad is happening");

=head1 DESCRIPTION

This is a subclass of L<Log::Dispatch::Email> that implements the send_email
method using the L<Mail::Sender> module.

=head1 AUTHOR

Dave Rolsky <autarch@urth.org>

=head1 COPYRIGHT AND LICENSE

This software is Copyright (c) 2011 by Dave Rolsky.

This is free software, licensed under:

  The Artistic License 2.0 (GPL Compatible)

=cut


__END__

